package com.ahp.user.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ahp.constants.ApplicationConstants;
import com.ahp.constants.LoggedInProfile;
import com.ahp.log.AhpLogFactory;

public class AuthenticationModel {
	
	private static final Logger logger = AhpLogFactory.getLogger(Level.ALL);
	
	public static boolean login(String username,String password){
		File file = new File("C:\\Documents and Settings\\ekansh.kumar\\workspace\\AHP\\src\\com\\ahp\\user\\model\\UserPasswords.properties");
		if(file.exists()){
			Properties properties = new Properties();
			try {
				properties.load(new FileInputStream(file));
			} catch (FileNotFoundException e) {
				logger.log(Level.SEVERE, "User Name Passwords file missing", e);
			} catch (IOException e) {
				logger.log(Level.SEVERE, "User Name Passwords file could not be read", e);
			}
			String pwd = properties.getProperty(username);
			if(pwd != null && pwd.equals(password))
				return true;
		}
		return false;
	}
	
	public static LoggedInProfile loggedInProfile(String username){
		if(username==null){
			return LoggedInProfile.NONE;
		}else if(ApplicationConstants.ADMIN_USER_NAME.equals(username)){
			return LoggedInProfile.ADMIN;
		}else{
			return LoggedInProfile.MERCHANT;
		}
	}

}
